package com.mt.fpb.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.mapper.WxProductMapper;
import com.mt.fpb.mapper.WxProductOrderMapper;
import com.mt.fpb.model.WxProduct;
import com.mt.fpb.model.WxProductOrder;
import com.mt.fpb.utils.ParseXml;
import com.vbao.service.trade.utils.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * FileName: GetVbao
 * Author:   wang
 * Date:     2021/8/24 10:58
 */
@Controller
@RequestMapping(value = "getVbao")
public class GetVbaoController {

    @Autowired
    private WxProductOrderMapper wxProductOrderMapper;

    @Autowired
    private WxProductMapper wxProductMapper;


    @PostMapping(value = "get")
    @ResponseBody
    public String getVbao(String reqmsg ){
        System.out.println("12315151");
        System.out.println(reqmsg);
        if (null != reqmsg && reqmsg.length() > 0) {
            String returnOriDKPayxml = "";// 返回明文报文
            try {
                // 进行解密
                returnOriDKPayxml = Base64.decode(reqmsg,
                        "UTF8");
                System.out.println("解密后报文" + returnOriDKPayxml);
                //对返回过来的xml报文进行解析
                Map map =ParseXml.parseNotifyUrlXml(returnOriDKPayxml);
                String orderNo = (String) map.get("OrderNo");
                System.out.println("orderNo:"+orderNo);
                String payTimeStr = (String) map.get("PayTime");
                String payMoneyStr = (String) map.get("OrderAmount");
                System.out.println("payMoneyStr:"+payMoneyStr);
                float payMoney= Float.parseFloat(payMoneyStr);
                System.out.println("payMoney:"+payMoney);
                //获取报文中返回过来的支付状态
                String status = (String) map.get("PayStatus");
                System.out.println("报文返回的支付状态status:"+status);


                StringBuffer sb = new StringBuffer(payTimeStr);
                String productName = (String) map.get("productName");
                String id= (String) map.get("Ext1");
                System.out.println("id:"+id);


                sb.insert(4,"-").insert(7,"-").insert(10," ").insert(13,":")
                        .insert(16,":");
                System.out.println("sb:"+sb);
                //根据报文中的产品名称查询产品表
                QueryWrapper<WxProduct> queryWrapper = new QueryWrapper();
                queryWrapper.eq("product_id",id);
                WxProduct wxProduct = wxProductMapper.selectOne(queryWrapper);
                long lockTime  = wxProduct.getLockTime();
                System.out.println("lockTime:"+lockTime);

                //再将转换好的stringBuffer转成string
                String s = sb.toString();
                System.out.println("转换前的payTime:"+s);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date payTime = null;
                try {
                    payTime = simpleDateFormat.parse(s);
                    System.out.println("转换后payTime:"+payTime);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(payTime);
                    calendar.add(Calendar.DATE,1);
                    calendar.set(Calendar.HOUR_OF_DAY,0);
                    calendar.set(Calendar.MINUTE,0);
                    calendar.set(Calendar.SECOND,0);
                    Date effectiveTime = calendar.getTime();
                    long payTimes=effectiveTime.getTime();
                    long timps=payTimes+(lockTime*24*60*60*1000);
                    //结束时间
                    Date endTime=new Date(timps);

                    WxProductOrder wxProductOrder = new WxProductOrder();
                    wxProductOrder.setOrderStatus(status);
                    //更新的开始时间
                    wxProductOrder.setStartTime(payTime);
                    //更新支付金额
                    wxProductOrder.setPayMoney(payMoney);
                    //更新生效时间
                    wxProductOrder.setEffectiveTime(effectiveTime);
                    //订单开始时收益金额就等于支付金额
                    wxProductOrder.setOrderMoney(payMoney);
                    //更新结束时间
                    wxProductOrder.setEndTime(endTime);
                    QueryWrapper<WxProductOrder> q = new QueryWrapper<>();
                    q.eq("id",orderNo);
                    //解析后，更新数据库中的订单状态
                    int result = wxProductOrderMapper.update(wxProductOrder,q);
                    System.out.println(result);

                } catch (ParseException e) {
                    e.printStackTrace();
                }


                // 进行验签
                return "OK";
            } catch (UnsupportedEncodingException e) {
                System.out.println("解析密文报文有误:" + e.getMessage());
            }
        }
        return "NO";
    }

}

